#include "../include/FXAPI.h"
#include "../include/FXAPIModule.h"

#include <stdio.h>

#include <iostream>
using namespace std;

#ifndef _WIN32

#include <unistd.h>
char * get_exe_path( char * buf, int count)
{

cout<<">>>Enter get_exe_path"<<endl;
	int i;
	int rslt = readlink("/proc/self/exe", buf, count);
	if (rslt < 0 || (rslt >= count))
	{
		cout<<">>>Error: get_exe_path"<<endl;
		return NULL;
	}
cout<<rslt<<endl;
	buf[rslt] = '\0';
	for (i = rslt; i > 0; i--)
	{
		if (buf[i] == '/')
		{
			buf[i + 1] = '\0';
			break;
		}
	}
cout<<buf<<endl;
	return buf;

}
#endif

CFXAPIModule::CFXAPIModule()
{
	
//Create();
}
void CFXAPIModule::Create()
{

	CPDF_ModuleMgr::Create();
	m_pModuleMgr = CPDF_ModuleMgr::Get();


	// set cjk.bin path
#ifdef _WIN32
	/*
	int nLen = 1024;
	char * szDLLName = NULL;
	szDLLName = new char[nLen];
	memset(szDLLName, 0, (nLen)*sizeof(char));
	HMODULE hModule = ::GetModuleHandleA("PDF2Bin.dll");
	::GetModuleFileNameA(hModule, szDLLName, nLen);
	char szDriver[_MAX_DRIVE];
	char szDir[_MAX_DIR];
	char szFName[_MAX_FNAME];
	char szExt[_MAX_EXT];
	_splitpath_s(szDLLName, szDriver, _MAX_DRIVE,
							szDir, _MAX_DIR,
							szFName, _MAX_FNAME,
							szExt, _MAX_EXT);
	CFX_ByteString bstr;
	bstr = szDriver; bstr += szDir;// bstr += FX_LPCSTR("fpdfcjk.bin");
	CPDF_ModuleMgr::Get()->SetModulePath(ADDIN_NAME_CJK, FX_LPCSTR(bstr));
	if (NULL != szDLLName)
		delete [] szDLLName;
		*/
#else

	char path[1024];
	memset(path, 0, sizeof(path));
	get_exe_path(path, 1024);
	CPDF_ModuleMgr::Get()->SetModulePath(ADDIN_NAME_CJK, path);

#endif
	m_pCodecModule = CCodec_ModuleMgr::Create();
	m_pModuleMgr->SetCodecModule(m_pCodecModule);


	CFX_GEModule::Create();
	CFX_GEModule::Get()->SetCodecModule(m_pCodecModule);

	m_pModuleMgr->InitPageModule();
	m_pModuleMgr->InitRenderModule();
	
	m_pModuleMgr->LoadEmbeddedGB1CMaps();
	m_pModuleMgr->LoadEmbeddedCNS1CMaps();
	m_pModuleMgr->LoadEmbeddedJapan1CMaps();
	m_pModuleMgr->LoadEmbeddedKorea1CMaps();

}
void CFXAPIModule::Destroy()
{
	if(NULL != m_pModuleMgr)
	{
		m_pModuleMgr->Destroy();
		m_pModuleMgr = NULL;
	}
	CFX_GEModule::Destroy();
	if(NULL != m_pCodecModule)
	{
		m_pCodecModule->Destroy();
		m_pCodecModule = NULL;
	}
}
CFXAPIModule::~CFXAPIModule()
{
//	Destroy();
}


//CFXAPIModule g_FXAPIModule;
////////////////////////////////////////////////////////////////////////////
//
